// SPU管理模块的接口
import request from "@/utils/request";
import type {
  HasSpuResponseData,
  AllTradeMark,
  SpuHasImg,
  SaleAttrResponseData,
  HasSaleAttrResponseData,
} from "./type";
enum API {
  // 获取已有的SPU数据
  HASSPU_URL = "/admin/product/",
  // 获取全部品牌的数据
  ALLTRADEMARK_URL = "/admin/product/baseTrademark/getTrademarkList",
  // 获取照片
  IMAGE_URL = "/admin/product/spuImageList/",
  // 获取销售属性
  SPUHASSALEATTR_URL = "/admin/product/spuSaleAttrList/",
  // 获取整个项目全部的销售属性[颜色,版本,尺码]
  ALLSALEATTR_URL = "/admin/product/baseSaleAttrList",
  // 添加SPU
  ADDSPU_URL = "/admin/product/saveSpuInfo",
  // 修改SPU
  UPDATESPU_URL = "/admin/product/updateSpuInfo",
  // 删除SPU
  DELETESPU_URL = "/admin/product/deleteSpu/",
  // 追加一个新的SKU地址
  ADDSKU_URL = "/admin/product/saveSkuInfo",
  // 查看spu下的sku
  SKUINFO_URL = "/admin/product/findBySpuId/",
}

// 获取已有数据方法
export const reqHasSpu = (
  page: number,
  limit: number,
  category3Id: number | string
) =>
  request.get<any, HasSpuResponseData>(
    API.HASSPU_URL + `${page}/${limit}?category3Id=${category3Id}`
  );

// 获取全部SPU品牌数据
export const reqAllTrademark = () =>
  request.get<any, AllTradeMark>(API.ALLTRADEMARK_URL);

// 获取图片
export const reqSpuImageList = (spuId: number) =>
  request.get<any, SpuHasImg>(API.IMAGE_URL + spuId);

// 获取销售属性
export const reqSpuHasSaleAttr = (spuId: number) =>
  request.get<any, SaleAttrResponseData>(API.SPUHASSALEATTR_URL + spuId);

// 获取全部的销售属性值
export const reqAllSaleAttr = () =>
  request.get<any, HasSaleAttrResponseData>(API.ALLSALEATTR_URL);

// 添加或更新SPU
// data:新增的spu或已有的spu
export const reqAddSpuOrUpdateSpu = (data: any) => {
  if (data.id) {
    return request.post<any, any>(API.UPDATESPU_URL, data);
  } else {
    return request.post<any, any>(API.ADDSPU_URL, data);
  }
};

// 删除SPU
export const reqDeleteSpu = (spuId: number) =>
  request.delete<any, any>(API.DELETESPU_URL + spuId);

// 添加SKU请求方法
export const reqAddSku = (data: any) =>
  request.post<any, any>(API.ADDSKU_URL, data);

// 查询sku方法
export const reqSkuList = (spuId: number) =>
  request.get<any, any>(API.SKUINFO_URL + spuId);
